<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="登录IP" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入登录IP"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <y-select
          v-model="queryParams.status"
          placeholder="请选择登录状态"
          :options="sys_success_fail"
        />
      </el-form-item>
      <el-form-item label="登录时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 对象操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sys:loginlog:delete']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          @click="handleClean"
          v-hasPermi="['sys:loginlog:delete']"
          >清空</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Unlock"
          :disabled="single"
          @click="handleUnlock"
          v-hasPermi="['sys:loginlog:unlock']"
          >解锁</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <y-export
          :url="'sys/loginlog/export/'"
          :ids="ids"
          :filename="'operlog_data'"
          v-hasPermi="['sys:loginlog:export']"
        />
      </el-col>
      <y-right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      />
    </el-row>

    <!-- 列表展示 -->
    <el-table
      ref="loginLogRef"
      v-loading="loading"
      row-key="id"
      :data="loginLogList"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        reserve-selection
      />
      <el-table-column label="序号" width="100" align="center" type="index">
        <template #default="scope">
          {{ scope.$index + (queryParams.page - 1) * queryParams.limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="编号"
        align="center"
        prop="id"
        width="300"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="用户名称"
        align="center"
        prop="username"
        :show-overflow-tooltip="true"
        sortable="custom"
        :sort-orders="['descending', 'ascending']"
        v-if="columns[1].visible"
      />
      <el-table-column
        label="登录IP"
        align="center"
        prop="ip"
        :show-overflow-tooltip="true"
        v-if="columns[2].visible"
      />
      <el-table-column
        label="操作系统"
        align="center"
        prop="os"
        :show-overflow-tooltip="true"
        v-if="columns[3].visible"
      />
      <el-table-column
        label="浏览器"
        align="center"
        prop="browser"
        :show-overflow-tooltip="true"
        v-if="columns[4].visible"
      />
      <el-table-column
        label="访问时间"
        align="center"
        prop="create_time"
        sortable="custom"
        :sort-orders="['descending', 'ascending']"
        width="180"
        v-if="columns[5].visible"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <y-pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.limit"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="LoginLog">
import {
  list,
  delLoginLog,
  cleanLoginLog,
  unlockLoginLog,
} from "@/api/sys/loginlog";

const { proxy } = getCurrentInstance();
const { sys_success_fail } = proxy.useDict("sys_success_fail");

const loginLogList = ref([]);
const loading = ref(true);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const selectName = ref("");
const total = ref(0);
const dateRange = ref([]);
const defaultSort = ref({ prop: "loginTime", order: "descending" });

// 列显隐信息
const columns = ref([
  { key: 0, label: `编号`, visible: false },
  { key: 1, label: `用户名称`, visible: true },
  { key: 2, label: `登录IP`, visible: true },
  { key: 3, label: `操作系统`, visible: true },
  { key: 4, label: `浏览器`, visible: true },
  { key: 5, label: `访问时间`, visible: true },
]);

// 查询参数
const queryParams = ref({
  page: 1,
  limit: 10,
  ip: undefined,
  username: undefined,
  status: undefined,
  orderByColumn: undefined,
  isAsc: undefined,
});

/** 查询登录日志列表 */
function getList() {
  loading.value = true;
  list(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      loginLogList.value = response.data;
      total.value = response.total;
      loading.value = false;
    }
  );
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.page = 1;
  proxy.$refs["loginLogRef"].sort(
    defaultSort.value.prop,
    defaultSort.value.order
  );
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
  single.value = selection.length != 1;
  selectName.value = selection.map((item) => item.username);
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
  queryParams.value.orderByColumn = column.prop;
  queryParams.value.isAsc = column.order;
  getList();
}

/** 删除按钮操作 */
async function handleDelete(row) {
  try {
    if (row.id) {
      await proxy.$modal.confirm(`确认是否删除 "${row.name}" ？`);
    } else {
      await proxy.$modal.confirm("确认是否删除所有已勾选信息？");
    }
    ids.value = row.id ? [row.id] : ids.value;
    await delLoginLog(ids.value);
    proxy.$modal.msgSuccess("删除成功");
    proxy.$refs.tableRef.clearSelection();
    getList();
  } catch (error) {
    console.error(error);
  }
}

/** 清空按钮操作 */
function handleClean() {
  proxy.$modal
    .confirm("是否确认清空所有登录日志数据项?")
    .then(function () {
      return cleanLoginLog();
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("清空成功");
    })
    .catch(() => {});
}

/** 解锁按钮操作 */
function handleUnlock() {
  const username = selectName.value;
  proxy.$modal
    .confirm('是否确认解锁用户"' + username + '"数据项?')
    .then(function () {
      return unlockLoginLog(username);
    })
    .then(() => {
      proxy.$modal.msgSuccess("用户" + username + "解锁成功");
    })
    .catch(() => {});
}

onMounted(() => {
  getList();
});
</script>
